//
//  TJNewfeatureViewController.swift
//  WeiBo
//
//  Created by TanJian on 15/12/8.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

class TJNewfeatureViewController: UIViewController,UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()


        setupUI()
    }
    
    
    private func setupUI() {
        
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5, y: scrollView.frame.height-100, width: 0, height: 0)
        view.addSubview(pageControl)
        
        let count = 4
        for i in 0..<count {
            
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            
            imageView.frame = CGRect(x: CGFloat(i)*scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            
            
            //如果是最后一页，设置内容
            if i == count-1 {
                setupLastPage(imageView)
            }

        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.frame.width, height: 0)
        pageControl.numberOfPages = count
        
    }
    
    private func setupLastPage(lastPage:UIView) {
        
        lastPage.userInteractionEnabled = true
        enterButton.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        sharedButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMidY(enterButton.frame)-30)
        lastPage.addSubview(enterButton)
        lastPage.addSubview(sharedButton)
        
        
    }
    
    
    
    private lazy var scrollView:UIScrollView = {
        
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false

        return scrollView
    }()
    
    private lazy var enterButton:UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setTitle("进入微博", forState: .Normal)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "clickEnterBtn", forControlEvents: .TouchUpInside)
        return button
        
    }()
    
    @objc private func clickEnterBtn() {
        print("clickEnterBtn")
        NSNotificationCenter.defaultCenter().postNotificationName(TJSwitchController, object: nil)
    }
    
    //分享微博按钮
    private lazy var sharedButton:UIButton = {
        
        let button = UIButton()
        
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setTitle("分享到微博", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "clickShareBtn:", forControlEvents: .TouchUpInside)
        return button
        
    }()
    
    @objc private func clickShareBtn(button:UIButton) {
        button.selected = !button.selected
    }

    private lazy var pageControl:UIPageControl = {
       
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        
        return pageControl
        
    }()
    

    //scrollview的拖动代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        pageControl.currentPage = (Int)(scrollView.contentOffset.x/scrollView.frame.width+0.5)
    }
 

}









